package com.definesys.smecrailtransit.module.MonthForm.PopWindowAdapter;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.base.SmecBaseRecyclerViewHolder;
import com.definesys.smecrailtransit.databinding.LayoutCarePlanItemBinding;
import com.definesys.smecrailtransit.databinding.LayoutPopNullDataBinding;
import com.definesys.smecrailtransit.module.MonthForm.bean.CarePlanBean;

import java.util.ArrayList;

/**
 * Created by cjy on 2017/8/28.
 */

public class PopCarePlanAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder> {
    private static int DATA_NULL = -1;
    private static int DATA_ALL = 1;
    private ArrayList<CarePlanBean> list;
    public int start;

    public PopCarePlanAdapter(){
        this.list = new ArrayList<>();
        this.start = 0;
    }

    public void setList(ArrayList<CarePlanBean> list){
        if(list != null && list.size() != 0){
            this.list = list;
        }else{
            this.list = new ArrayList<>();
        }
        notifyDataSetChanged();
    }

    public ArrayList<CarePlanBean> getList(){
        return list;
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == DATA_ALL){
            LayoutCarePlanItemBinding layoutCarePlanItemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext())
                    , R.layout.layout_care_plan_item,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutCarePlanItemBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutCarePlanItemBinding);
            return smecBaseRecyclerViewHolder;
        }else{
            LayoutPopNullDataBinding layoutPopNullDataBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext())
                    , R.layout.layout_pop_null_data,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                    SmecBaseRecyclerViewHolder(layoutPopNullDataBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(layoutPopNullDataBinding);
            return smecBaseRecyclerViewHolder;
        }
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, int position) {
        holder.setIsRecyclable(false);
        if(holder.getViewType() == DATA_ALL){
            LayoutCarePlanItemBinding viewDataBinding=(LayoutCarePlanItemBinding)holder.getBinding();
            viewDataBinding.man.setText(list.get(position).getMntMan() != null ? list.get(position).getMntMan() : "");
            viewDataBinding.time.setText((list.get(position).getMntTime() != null ? list.get(position).getMntTime() : "") +
                    (list.get(position).getTimeRange() != null ? list.get(position).getTimeRange() : ""));
            viewDataBinding.phone.setText(list.get(position).getPhoneNum() != null ? list.get(position).getPhoneNum() : "");
        }
    }

    @Override
    public int getItemCount() {
        if(start == 1){
            if(this.list.size() == 0){
                return 1;
            }else{
                return this.list.size();
            }
        }else{
            return this.list.size();
        }

    }

    @Override
    public int getItemViewType(int position) {
        if(this.list.size() == 0){
            return DATA_NULL;
        }else{
            return DATA_ALL;
        }
    }
}
